package offer_test_20;

//输入一个矩阵，按照从外向里以顺时针的顺序依次打印出每一个数字。
public class twenty_PrintMatrix {
	public static void main(String[] args) {
//		int[][] array = { { 1, 2, 3,}, { 5, 6, 7, 8 }, { 9, 10, 11, 12 },
//				{ 13, 14, 15, 16 } };
//		int[][] array={{1,2,3},{4,5,6},{7,8,9}};
		int[][] array={{1}};
		 printMatixClockwisely(array);
	}

	public static void printMatixClockwisely(int[][] array) {
		if(array==null){
			System.exit(0);
		}
		int length = array.length;
		int width = array[0].length;
		int[][] tag = new int[length][width];
		if (length > 0 && width > 0) {
			for (int i = 0; i < width; i++) {
				if (tag[0][i] == 0) {
					System.out.print(array[0][i] + " ");
					tag[0][i] = 1;
				}
			}
			for (int i = 1; i < length - 1; i++) {
				if (tag[i][width - 1] == 0) {
					System.out.print(array[i][width - 1] + " ");
					tag[i][width - 1] = 1;
				}

			}
			for (int i = width - 1; i > 0; i--) {
				if (tag[length - 1][i] == 0) {
					System.out.print(array[length - 1][i] + " ");
					tag[length - 1][i] = 1;
				}
			}
			for (int i = length - 1; i > 0; i--) {

				if (tag[i][0] == 0) {
					System.out.print(array[i][0] + " ");
					tag[i][0] = 1;
				}
			}
		}
		if (length > 2 && width > 2) {
			int[][] matrix = new int[length - 2][width - 2];
			for (int i = 0; i < matrix.length; i++) {
				for (int j = 0; j < matrix[0].length; j++) {
					matrix[i][j] = array[i + 1][j + 1];
				}
			}
			printMatixClockwisely(matrix);
		}
	}
}
